/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.inspur.edp.lcm.metadata.core.manager;

import com.inspur.edp.lcm.metadata.api.mvnEntity.MetadataInfoWithVersion;
import com.inspur.edp.lcm.metadata.core.RestTemplateService;
import com.inspur.edp.lcm.metadata.inner.api.IdeSettingService;
import com.inspur.edp.lcm.metadata.inner.api.data.IndexServer;
import com.inspur.edp.lcm.metadata.inner.api.utils.IdeSettingUtils;
import java.util.Objects;
import org.springframework.util.CollectionUtils;

import java.util.List;

public class IndexServerManager {
    private static IndexServerManager instance;

    public static IndexServerManager getInstance() {
        if (instance == null) {
            instance = new IndexServerManager();
        }
        return instance;
    }

    public String getPageWebapiOfIndexServer() {
        return getIndexServerUrl() + "/page";
    }

    public String getPageWebapiOfIndexServerWithProcessMode() {
        return getIndexServerWithProcessModeUrl() + "/page";
    }

    public String getMetadataWebapiOfIndexServer() {
        return getIndexServerUrl() + "/metadataId";
    }

    public String getIndexServerUrl() {
        IdeSettingService ideSettingService = IdeSettingUtils.getIdeSettingService();
        if(Objects.isNull(ideSettingService)){
            return null;
        }
        IndexServer metadataIndexServer = ideSettingService.getIndexServer();
        if(Objects.isNull(metadataIndexServer)){
            return null;
        }
        return "http://" + metadataIndexServer.getIp() + ":" + metadataIndexServer.getPort() + "/api/v1/metadataindex/mvn";
    }

    public String getIndexServerWithProcessModeUrl() {
        return getIndexServerUrl() + "/withprocessmode";
    }

    public String getTestStatusWebapiOfIndexServer(String ip, String port) {
        return "http://" + ip + ":" + port + "/api/v1/metadataindex/mvn/metadataId?metadataId=test";
    }

    public String getPackageNameByGA(String groupId, String artifactId) {
        String metadataWebapiOfIndexServer = IndexServerManager.getInstance().getIndexServerWithProcessModeUrl();
        List<MetadataInfoWithVersion> metadataInfoWithVersionList = RestTemplateService.instance.getObjectByGet(metadataWebapiOfIndexServer);
        if (CollectionUtils.isEmpty(metadataInfoWithVersionList)) {
            return null;
        }
        // 在索引服务器中找到匹配，返回，找不到返回null
        for (MetadataInfoWithVersion metadataInfoWithVersion : metadataInfoWithVersionList) {
            if (!CollectionUtils.isEmpty(metadataInfoWithVersion.getMavenInfos())
                    && metadataInfoWithVersion.getMavenInfos().get(0).getMavenPackageGroupId().equals(groupId)
                    && metadataInfoWithVersion.getMavenInfos().get(0).getMavenPackageArtifactId().equals(artifactId)) {
                return metadataInfoWithVersion.getPackageCode();
            }
        }
        return null;
    }

}
